package top.songxs.yygh.service.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.songxs.yygh.enums.OrderStatusEnum;
import top.songxs.yygh.model.order.OrderInfo;
import top.songxs.yygh.service.order.mapper.OrderInfoMapper;
import top.songxs.yygh.service.order.service.OrderInfoService;
import top.songxs.yygh.vo.order.OrderQueryVo;

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Override
    public OrderInfo getOrderInfo(String orderId) {
        OrderInfo orderInfo = baseMapper.selectById(orderId);
        this.packOrderInfo(orderInfo);
        return orderInfo;
    }

    @Override
    public Page<OrderInfo> getpageList(Long pageNum, Long pageSize, OrderQueryVo orderQueryVo) {

        Page<OrderInfo> pages = new Page<>(pageNum,pageSize);
        //根据用户id查询用户订单列表
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        Long userId = orderQueryVo.getUserId();
        if (userId!=null)
            queryWrapper.eq("user_id",userId);
        //条件2 根据选择的就诊人 查询订单里列表
        Long patientId = orderQueryVo.getPatientId();
        if (patientId!=null)
            queryWrapper.eq("patient_id",patientId);
        //条件3 根据订单状态过滤订单
        String orderStatus = orderQueryVo.getOrderStatus();
        if (!StringUtils.isEmpty(orderStatus))
            queryWrapper.eq("order_status",orderStatus);

        //调用mapper中的分页方法进行查询
        Page<OrderInfo> orderInfoIPage = baseMapper.selectPage(pages, queryWrapper);

        orderInfoIPage.getRecords().forEach(this::packOrderInfo);


        return orderInfoIPage;
    }




    private void packOrderInfo(OrderInfo orderInfo) {
        String statusNameByStatus = OrderStatusEnum.getStatusNameByStatus(orderInfo.getOrderStatus());
        orderInfo.getParam().put("orderStatusString", statusNameByStatus);

    }
}
